{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 歌曲序列建模\n",
    "### 从word2vec到song2vec\n",
    "把歌曲的id序列取出来，类比于分完词后的句子，送到word2vec中去学习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "#coding: utf-8\n",
    "import multiprocessing\n",
    "import gensim\n",
    "import sys\n",
    "from random import shuffle\n",
    "\n",
    "def parse_playlist_get_sequence(in_line, playlist_sequence):\n",
    "    song_sequence = []\n",
    "    contents = in_line.strip().split(\"\\t\")\n",
    "    # 解析歌单序列\n",
    "    for song in contents[1:]:\n",
    "        try:\n",
    "            song_id, song_name, artist, popularity = song.split(\"::\")\n",
    "            song_sequence.append(song_id)\n",
    "        except:\n",
    "            print (\"song format error\")\n",
    "            print (song+\"\\n\")\n",
    "    for i in range(len(song_sequence)):\n",
    "        shuffle(song_sequence)\n",
    "        playlist_sequence.append(song_sequence)\n",
    "\n",
    "\n",
    "def train_song2vec(in_file, out_file):\n",
    "    #所有歌单序列\n",
    "    playlist_sequence = []\n",
    "    #遍历所有歌单\n",
    "    for line in open(in_file, encoding='utf-8'):\n",
    "        parse_playlist_get_sequence(line, playlist_sequence)\n",
    "    #使用word2vec训练\n",
    "    cores = multiprocessing.cpu_count()\n",
    "    print (\"using all \"+str(cores)+\" cores\")\n",
    "    print (\"Training word2vec model...\")\n",
    "    model = gensim.models.Word2Vec(sentences=playlist_sequence, size=150, min_count=3, window=7, workers=cores)\n",
    "    print (\"Saving model...\")\n",
    "    model.save(out_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "song format error\n",
      "1870957::彩云国物语 セカンドシリーズ::君を想う::梁邦彦::80.0\n",
      "\n",
      "song format error\n",
      "4965888::桃华月惮::龙皇-リュウオウ-::多田彰文::25.0\n",
      "\n",
      "song format error\n",
      "456177::true tears::一阵の风::菊地創::95.0\n",
      "\n",
      "song format error\n",
      "22642373::\n",
      "\n",
      "song format error\n",
      " FAIRY TAIL メインテーマ -Slow ver.-::高梨康治::95.0\n",
      "\n",
      "song format error\n",
      "31563610::\n",
      "\n",
      "song format error\n",
      "苍之礼赞::花之祭P::60.0\n",
      "\n",
      "song format error\n",
      "4954593::リズム天国全曲集::恋の実験室::V.A.::55.0\n",
      "\n",
      "song format error\n",
      "4954596::リズム天国全曲集::シンクロ::V.A.::60.0\n",
      "\n",
      "song format error\n",
      "31654811::\n",
      "\n",
      "song format error\n",
      "American Cowboys::Tim Wynn::65.0\n",
      "\n",
      "song format error\n",
      "19169096::\n",
      "\n",
      "song format error\n",
      " Time to Say Goodbye (Con te partirò)::Sarah Brightman::100.0\n",
      "\n",
      "song format error\n",
      "31563610::\n",
      "\n",
      "song format error\n",
      "苍之礼赞::花之祭P::60.0\n",
      "\n",
      "song format error\n",
      "31563610::\n",
      "\n",
      "song format error\n",
      "苍之礼赞::花之祭P::60.0\n",
      "\n",
      "song format error\n",
      "31563610::\n",
      "\n",
      "song format error\n",
      "苍之礼赞::花之祭P::60.0\n",
      "\n",
      "song format error\n",
      "19169096::\n",
      "\n",
      "song format error\n",
      " Time to Say Goodbye (Con te partirò)::Sarah Brightman::100.0\n",
      "\n",
      "song format error\n",
      "376653::野弧禅狂叱(宿香之战)\n",
      "\n",
      "song format error\n",
      "::霹雳英雄::5.0\n",
      "\n",
      "song format error\n",
      "374524::赎?罪\n",
      "\n",
      "song format error\n",
      "赎罪岩::霹雳英雄::15.0\n",
      "\n",
      "song format error\n",
      "31563610::\n",
      "\n",
      "song format error\n",
      "苍之礼赞::花之祭P::65.0\n",
      "\n",
      "song format error\n",
      "37610597::ダウンタウン熱血物語::公園/河原にて~ひとときのやすらぎ~::V.A.::75.0\n",
      "\n",
      "song format error\n",
      "37610748::くにおくんの熱血サッカーリーグ::ねっけつ たいふーん♪::V.A.::80.0\n",
      "\n",
      "song format error\n",
      "37610755::くにおくんの熱血サッカーリーグ::てくのす じゃぱん かっぷの てーま♪::V.A.::75.0\n",
      "\n",
      "song format error\n",
      "37610745::くにおくんの熱血サッカーリーグ::ゲームモード選択::V.A.::75.0\n",
      "\n",
      "song format error\n",
      "37610643::ダウンタウン熱血行進曲 それゆけ大運動会::オープニングファンファーレ::V.A.::70.0\n",
      "\n",
      "song format error\n",
      "33054290::\n",
      "\n",
      "song format error\n",
      "Heartbeats::Dabin::90.0\n",
      "\n",
      "song format error\n",
      "405599088::Make Them Wheels Roll\n",
      "\n",
      "song format error\n",
      "::SAFIA::100.0\n",
      "\n",
      "song format error\n",
      "424496188::大王叫我来巡山 - （原唱：\n",
      "\n",
      "song format error\n",
      " 贾乃亮/贾云馨）::流浪的蛙蛙::65.0\n",
      "\n",
      "song format error\n",
      "19169096::\n",
      "\n",
      "song format error\n",
      " Time to Say Goodbye (Con te partirò)::Sarah Brightman::100.0\n",
      "\n",
      "song format error\n",
      "26902203::What’s your name? (collaboration with 壇蜜)\n",
      "\n",
      "song format error\n",
      "::SoulJa::100.0\n",
      "\n",
      "song format error\n",
      "33054290::\n",
      "\n",
      "song format error\n",
      "Heartbeats::Dabin::95.0\n",
      "\n",
      "song format error\n",
      "4954596::リズム天国全曲集::シンクロ::V.A.::60.0\n",
      "\n",
      "song format error\n",
      "32272105::\n",
      "\n",
      "song format error\n",
      "Wonderful Love (DJ Raf Remix)::Money Penny::95.0\n",
      "\n",
      "song format error\n",
      "33054290::\n",
      "\n",
      "song format error\n",
      "Heartbeats::Dabin::95.0\n",
      "\n",
      "song format error\n",
      "427373827::Champions (From \"Hands of Stone\") \n",
      "\n",
      "song format error\n",
      "::Usher::30.0\n",
      "\n",
      "song format error\n",
      "29242687::「コード・エテスウェイ (Class::ETHES_WEI=>extends.COMMUNI_SAT/.)」::霜月はるか::70.0\n",
      "\n",
      "using all 4 cores\n",
      "Training word2vec model...\n",
      "Saving model...\n"
     ]
    }
   ],
   "source": [
    "song_sequence_file = \"./ori_data/popular.playlist\"\n",
    "model_file = \"./model/song2vec.model\"\n",
    "train_song2vec(song_sequence_file, model_file)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 预测的过程，实际上就是对某首歌曲，查找“最近”的歌曲（向量距离最近的歌曲）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pickle\n",
    "song_dic = pickle.load(open(\"./pro_data/popular_song.pkl\",\"rb\"))\n",
    "model_str = \"./model/song2vec.model\"\n",
    "model = gensim.models.Word2Vec.load(model_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "315958 那件疯狂的小事叫爱情\t袁泉\n",
      "28138980 为你我受冷风吹\t孙露\n",
      "247526 彗星的眼泪\t金莎\n",
      "5280395 慨古吟(琴歌)\t张铜霞\n",
      "31140395 一首简单的歌\t本兮\n",
      "27532150 Smoke Fly ft. JBo Escobar & Khaki\tAl Rocco\n",
      "440767373 メドゥーサ（美杜莎）\t月蝕原创音乐\n",
      "16323636 The Prayer\tAndrea Bocelli\n",
      "281436 夜曲\t彭芳\n",
      "5270404 渴望(二胡)\t群星\n"
     ]
    }
   ],
   "source": [
    "for song in list(song_dic.keys())[:10]:\n",
    "    print (song, song_dic[song])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Anaconda\\install\\lib\\site-packages\\ipykernel_launcher.py:3: DeprecationWarning: Call to deprecated `most_similar` (Method will be removed in 4.0.0, use self.wv.most_similar() instead).\n",
      "  This is separate from the ipykernel package so we can avoid doing imports until\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "368971 Ambulance of love\t脑浊\n",
      "\n",
      "相似歌曲 和 相似度 分别为:\n",
      "\t 新世界\t呼吸 0.8102102279663086\n",
      "\t 上苍保佑吃完了饭的人民\t张楚 0.8082322478294373\n",
      "\t 呀呀\t图腾 0.7943791151046753\n",
      "\t 昨日我从清晨开始等待\t钟立风 0.774426281452179\n",
      "\t 生命(Live) - live\t声音玩具 0.7557182312011719\n",
      "\t 两天\t许巍 0.7442638874053955\n",
      "\t 永远在一起\t飘乐队 0.7283462285995483\n",
      "\t 今夜\t许巍 0.7184534072875977\n",
      "\t 祖先的阴影\t超载 0.7170863747596741\n",
      "\t 我们走过的路\t天空 0.7092562913894653\n",
      "\n",
      "\n",
      "33599059 八秒之语\t洛天依\n",
      "\n",
      "相似歌曲 和 相似度 分别为:\n",
      "\t 乡村DISCO\tVOCALOID 0.6794760227203369\n",
      "\t 春雨\t乐正绫 0.649375319480896\n",
      "\t 远恋\t阿良良木健 0.6485384702682495\n",
      "\t 食之歌 VOCALOID Ver.\t泛音堂 0.6450830698013306\n",
      "\t 小幸运（Cover：田馥甄）\t星魂梦 0.6226800680160522\n",
      "\t 出格\t阿妍 0.6186865568161011\n",
      "\t 双向监禁\t洛天依 0.6172651052474976\n",
      "\t 山海默示录（洛天依版）\t小旭PRO 0.6170870661735535\n",
      "\t 甄姬\tVOCALOID 0.613639771938324\n",
      "\t 全世界都死了\t海鲜面 0.6097506284713745\n",
      "\n",
      "\n",
      "408332846 知足\t苏运莹\n",
      "\n",
      "相似歌曲 和 相似度 分别为:\n",
      "\t 垃圾车（cover 五月天）\t李昂星 0.714142918586731\n",
      "\t Happy Birth Day\t香蕉 0.7014893293380737\n",
      "\t 拥抱（Cover 五月天)\t橙大蕾蕾 0.6977276802062988\n",
      "\t 爱情的模样\t小平 0.6919869780540466\n",
      "\t 听不到(Live)\t梁静茹 0.6528257727622986\n",
      "\t 我就是这样的\t黄贯中 0.6462737917900085\n",
      "\t 拥抱（Cover 五月天）\t燕子姐姐弹吉他 0.6430615186691284\n",
      "\t 穿越时空遇见你\t萧亚轩 0.6319111585617065\n",
      "\t 神奇\t孙燕姿 0.6182767152786255\n",
      "\t 一个人的圣诞节\t张赫宣 0.6101440191268921\n",
      "\n",
      "\n",
      "34072696 酒馆小调\t洛天依\n",
      "\n",
      "相似歌曲 和 相似度 分别为:\n",
      "\t 菌裂\t言和 0.7842902541160583\n",
      "\t 妄想不到的恋曲\t烂兔子 0.748408317565918\n",
      "\t 女王\t洛天依 0.7209190726280212\n",
      "\t 清醒的梦 \tVilokun feat.言和 0.7056742906570435\n",
      "\t Mr 坷垃\t言和 0.7054739594459534\n",
      "\t 【心华】乌龟家的茶社【ick】\t缺钙体质ick 0.7002834677696228\n",
      "\t 偶像进行时\t言和 0.698686420917511\n",
      "\t 乐正绫 - 拉斯维加斯\t慕晓社 0.6933234333992004\n",
      "\t 流星之愿\t洛天依&言和 0.6909075975418091\n",
      "\t 病态的我\t洛天依 0.6879562139511108\n",
      "\n",
      "\n",
      "279713 十六夜的樱丘\t梦璟SAYA\n",
      "\n",
      "相似歌曲 和 相似度 分别为:\n",
      "\t 合金三国-沛县\t灰原穷 0.7301620841026306\n",
      "\t 岁月友情演唱会Live\t聂予词 0.7063505053520203\n",
      "\t 克罗地亚狂想曲（中文填词版）\t少年霜 0.6886762976646423\n",
      "\t 言君安\t倾夜 0.6869547963142395\n",
      "\t 凤鸣曲\t音频怪物 0.6675935387611389\n",
      "\t 相忘江湖\t玄觞 0.6578972935676575\n",
      "\t 伊人\t魏晨 0.6283127665519714\n",
      "\t 失落的遗迹:Lost Ruins ~ adventurers' tale~\tkaede 0.6206890344619751\n",
      "\t 【Moonlight组合】甜蜜具现式\tKBShinya 0.6184070110321045\n",
      "\t 老房子的故事【老歌搬家】\tWinky诗 0.6105965971946716\n",
      "\n",
      "\n",
      "33004911 听妈妈讲那过去的事情\t群星\n",
      "\n",
      "相似歌曲 和 相似度 分别为:\n",
      "\t 国旗多美丽\t群星 0.9948954582214355\n",
      "\t 劳动最光荣\t杨烁 0.9924089312553406\n",
      "\t 数青蛙\t群星 0.9401867985725403\n",
      "\t 儿童歌曲大联唱B\t群星 0.9401167631149292\n",
      "\t 母鸭带小鸭\t杨烁 0.937639594078064\n",
      "\t 嘀哩,嘀哩\t中央人民广播电台少年儿童合唱团 0.931469738483429\n",
      "\t 小小少年\t韩征 0.9063182473182678\n",
      "\t 世上只有妈妈好\t杨烁 0.893858790397644\n",
      "\t 真善美的小世界\t小蓓蕾组合 0.8826833963394165\n",
      "\t 我家住在北京城\t苑菁 0.8813650012016296\n",
      "\n",
      "\n",
      "26389372 水色\tUA\n",
      "\n",
      "相似歌曲 和 相似度 分别为:\n",
      "\t 最后の言い訳\t徳永英明 0.7948691248893738\n",
      "\t テルーの呗\t手嶌葵 0.7602044343948364\n",
      "\t もう君以外爱せない\tKinKi Kids 0.7559913992881775\n",
      "\t 氷点\t玉置浩二 0.7401448488235474\n",
      "\t 时の过ぎゆくままに\t沢田研二 0.7194046378135681\n",
      "\t 时代\t中島みゆき 0.7185744643211365\n",
      "\t 手紙 ~拝啓 十五の君へ~\tアンジェラ・アキ 0.6948644518852234\n",
      "\t あした\t中島みゆき 0.685759425163269\n",
      "\t MR.LONELY\t玉置浩二 0.6696567535400391\n",
      "\t Carcrashes [Album Version]\tStandfast 0.6691217422485352\n",
      "\n",
      "\n",
      "166471 别等离开才说爱我\t王志\n",
      "\n",
      "相似歌曲 和 相似度 分别为:\n",
      "\t 分爱  粤语版\t易欣 0.8755930066108704\n",
      "\t 反叛(Illegal Mix) - remix\t陈慧娴 0.873873233795166\n",
      "\t 背叛\t芭比 0.8577862977981567\n",
      "\t 罗盘上的指针\t群星 0.8541814088821411\n",
      "\t 最美丽的花\t王绎龙 0.8295896649360657\n",
      "\t 各种小曲各种嗨\t珊爷 0.8236122727394104\n",
      "\t 不要推我\t群星 0.7918290495872498\n",
      "\t 狂舞大麻\t群星 0.7873603105545044\n",
      "\t 真的不容易 (DJ阿圣 Remix)\t庄心妍 0.7646005749702454\n",
      "\t 看我72变\tM3 0.7498428225517273\n",
      "\n",
      "\n",
      "29414454 心中喜欢就说爱\t好妹妹乐队\n",
      "\n",
      "相似歌曲 和 相似度 分别为:\n",
      "\t 请你给我多一点点的温柔\t秦昊 0.8446059823036194\n",
      "\t 熟悉的拥抱 (Demo)\t好妹妹乐队 0.8162363767623901\n",
      "\t 秋诗篇篇 \t秦昊 0.8092836737632751\n",
      "\t 风从海面吹过来\t好妹妹乐队 0.792778491973877\n",
      "\t 军港之夜 \t秦昊 0.78443443775177\n",
      "\t 熟悉的拥抱\t好妹妹乐队 0.7571429014205933\n",
      "\t 心曲\t好妹妹乐队 0.7473146319389343\n",
      "\t 风又吹走了\t好妹妹乐队 0.7431649565696716\n",
      "\t 愿在秋天死去 (Demo)\t好妹妹乐队 0.7383122444152832\n",
      "\t 四季歌\t秦昊 0.7374083399772644\n",
      "\n",
      "\n",
      "347983 传奇 Legend\t春秋\n",
      "\n",
      "相似歌曲 和 相似度 分别为:\n",
      "\t 开始\t核聚变-G 0.8861181735992432\n",
      "\t 抢回一切\t岩浆乐队 0.85029536485672\n",
      "\t No One Can Change My Mind\t利事乐队 0.8492539525032043\n",
      "\t 武器\t浊乐队 0.809968888759613\n",
      "\t 【填翻】城池\t妖痴 0.786383867263794\n",
      "\t 杀手\t战斧 0.7508671879768372\n",
      "\t 梦魔\t大红袍 0.7423715591430664\n",
      "\t 梦已成\"血\"\t液氧罐头 0.7245020866394043\n",
      "\t 大鱼\t三火SAMA 0.7139255404472351\n",
      "\t 是什么，让我们留在这里？\t夜叉 0.7099397778511047\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "song_id_list = list(song_dic.keys())[1000:1500:50]\n",
    "for song_id in song_id_list:\n",
    "    result_song_list = model.most_similar(song_id)\n",
    "\n",
    "    print (song_id, song_dic[song_id])\n",
    "    print (\"\\n相似歌曲 和 相似度 分别为:\")\n",
    "    for song in result_song_list:\n",
    "        print (\"\\t\", song_dic[song[0]], song[1])\n",
    "    print (\"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
